package me.lcc.demo.utils;
/**
 * -用户登陆信息
 *
 */

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import me.lcc.demo.model.Account;

public class AccountUtils {
	public static Account currentLoginAccount() {
		Account acc = new Account();
		Object principl = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(principl instanceof UserDetails) {
			UserDetails currentUser = ((UserDetails)principl);
			acc.setUsername(currentUser.getUsername());
			acc.setPassword(currentUser.getPassword());
			
			StringBuilder sb = new StringBuilder();
			currentUser.getAuthorities().forEach(grantedAuthority->{
				sb.append(",");
				sb.append(grantedAuthority.getAuthority());
			});
			
			acc.setRole(sb.toString());
		}else {
			acc.setUsername(principl == null ? "No Account" : principl.toString());
		}
		return acc;
	}
}
